import { userLogin, getUserInfo, saveOrUpdateUser, changeUserPassword } from '../../api/user'
import { getToken, setToken, removeToken } from '../../utils/auth'

const getDefaultState = () => {
  return {
    token: getToken(),
    userName: '',
    userAccount: '',
    userPhone: '',
    userSex: '',
    userEmail: '',
    userAvatar: '',
    userAddress: ''
  }
}
const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USER_NAME: (state, userName) => {
    state.userName = userName
  },
  SET_USER_ACCOUNT: (state, userAccount) => {
    state.userAccount = userAccount;
  },
  SET_USER_PHONE: (state, userPhone) => {
    state.userPhone = userPhone;
  },
  SET_USER_SEX: (state, userSex) => {
    state.userSex = userSex;
  },
  SET_USER_EMAIL: (state, userEmail) => {
    state.userEmail = userEmail;
  },
  SET_USER_AVATAR: (state, userAvatar) => {
    state.userAvatar = userAvatar;
  },
  SET_USER_ADDRESS: (state, userAddress) => {
    state.userAddress = userAddress
  }
}

const actions = {
  // 用户登录
  async login({ commit }, userInfo) {
    let result = await userLogin(userInfo)
    if (result.code == 200) {
      commit('SET_TOKEN', result.data);
      setToken(result.data);
    } else {
      return Promise.reject(new Error('false'));
    }
  },
  // 用户注册或更新
  async saveOrUpdate({ commit }, userInfo) {
    let result = await saveOrUpdateUser(userInfo);
    if (result.code == 200) {
      uni.showToast({
        title: '信息提交成功',
        duration: 2000
      })
      commit('SET_TOKEN', result.data);
      setToken(result.data);
    } else {
      return Promise.reject(new Error('false'));
    }
  },
  // 用户密码修改
  async changePassword({ commit }, userInfo) {
    let result = await changeUserPassword(userInfo);
    if (result.code == 200) {
      uni.showToast({
        title: '密码修改成功',
        duration: 2000
      })
    } else {
      return Promise.reject(new Error('false'));
    }
  },
  // 获取用户信息
  async getInfo({ commit }) {
    let result = await getUserInfo();
    if (result.code == 200) {
      if (!result.data) {
        uni.showToast({
          title: '令牌已过期,请重新登录',
          duration: 2000
        })
        Promise.reject(new Error('false'));
      }
      const { userName, userAccount, userPhone, userSex, userEmail, userAvatar, userAddress } = result.data;
      commit('SET_USER_NAME', userName);
      commit('SET_USER_ACCOUNT', userAccount);
      commit('SET_USER_PHONE', userPhone);
      commit('SET_USER_SEX', userSex);
      commit('SET_USER_EMAIL', userEmail);
      commit('SET_USER_AVATAR', userAvatar);
      commit('SET_USER_ADDRESS', userAddress);
      return;
    } else {
      return Promise.reject(new Error('false'));
    }
  },

  // 用户退出登录
  logout({ commit, state }) {
    removeToken() // must remove  token  first
    // 跳转到登录页(还没做)
    commit('RESET_STATE')
  },
  // 更新token
  updateToken({ commit }, data) {
    commit('SET_TOKEN', data.token);
    setToken(data.token);
  },
  // 移除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

